const path = require("path");
const fs = require("fs");
// 输入的版本号
const version = process.argv[2];
if (!version) {
  console.error("请输入要修改为的版本号");
  return;
}

const start = () => {
  const files = fs
    .readdirSync(".", {
      recursive: true,
    })
    .filter((file) => file.endsWith(`.csproj`))
    .map((file) => path.join(".", file));

  //   var file = files[0];
  let index = 0;
  for (var file of files) {
    let content = fs.readFileSync(file, "utf-8");
    const packageId = content.match(/<PackageId>(.*?)<\/PackageId>/)?.[1];
    const build = content.match(
      /<GeneratePackageOnBuild>(.*?)<\/GeneratePackageOnBuild>/
    )?.[1];
    if (packageId && build) {
      console.log(`开始修改: ${file}, PackageId: ${packageId}`);
      content = content.replace(
        /<Version>(.*?)<\/Version>/,
        `<Version>${version}</Version>`
      );
      fs.writeFileSync(file, content);
      index++;
    }
  }
  console.log(`共修改${index}个文件`);
};

start();
